package com.atguigu.tingshu.live.api;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public class Test {

    public static void main(String[] args) {
        //  鉴权key
        String txrtmp = "3df6a2b8b90c69391c43520be027b7bb";
        //    声明一个streamName
        String streamName = "room1";
        //    设置当前推流地址的有效期
        LocalDateTime localDateTime = LocalDateTime.now();
        long second = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
        Long txTime = second + 12 * 60 * 60;
        //    鉴权key
        String txSecret = getSafeUrl(txrtmp, streamName, txTime);
        //  webrtc://190649.push.tlivecloud.com/live/room1?txSecret=84a448c13a32a9c1062580c5bc49d512&txTime=65557E27
        //  webrtc://190649.push.tlivecloud.com/live/room1?txSecret=c060f9d6bb304e0e479b2735ccef9917&txTime=6554D657
        //  webrtc://190649.push.tlivecloud.com/live/room1?txSecret=08bd79d01fd4702e3e13de8f410565a7&txTime=6554D74C
        String pushUrl = "webrtc://190649.push.tlivecloud.com/live/"+streamName+"?"+txSecret;

        String pullUrl = "webrtc://mqxplay.atguigu.cn.txlivecdn.com/live/"+streamName+"?"+txSecret;
        System.out.println(txSecret);
        System.out.println("推流地址:"+pushUrl);
        //  webrtc://mqxplay.atguigu.cn/live/room1
        System.out.println("拉流地址:"+pullUrl);

        //  获取过期时间的秒;
        System.out.println(new Date().getTime()/1000);
        System.out.println(second);



    }

    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /*
     * KEY+ streamName + txTime
     */
    private static String getSafeUrl(String key, String streamName, long txTime) {
        String input = new StringBuilder().
                append(key).
                append(streamName).
                append(Long.toHexString(txTime).toUpperCase()).toString();

        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret = byteArrayToHexString(
                    messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return txSecret == null ? "" :
                new StringBuilder().
                        append("txSecret=").
                        append(txSecret).
                        append("&").
                        append("txTime=").
                        append(Long.toHexString(txTime).toUpperCase()).
                        toString();
    }

    private static String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }
}